package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.dal.IUserDataDAL;
import com.qire.manhua.model.viewModel.impl.AccountBindGuideViewModel;
import com.qire.manhua.model.vo.MobileAuthInfoVO;
import com.qire.manhua.model.vo.OkResponseVO;
import com.qire.manhua.model.vo.UserDetailVO;

public class AccountBindGuideService extends DataService {

    private final IUserDataDAL userDataDAL = WarehouseFactory.create(this, IUserDataDAL.class);

    private final AccountBindGuideViewModel viewModel;

    public AccountBindGuideService(AccountBindGuideViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 验证手机号码合法性
     * @param phoneNumber
     */
    public void authPhoneNumberBy(String phoneNumber) {
        userDataDAL.fetchMobileAuthInfo(phoneNumber);
    }
    @StrategyAnnotation(event = Url.v5_mobileAuthInfo)
    public void fetchMobileAuthInfoCallback(MobileAuthInfoVO mobileAuthInfoVO, boolean isCache) {
        if(ObjectUtil.isNull(mobileAuthInfoVO)) {
            return;
        }

        if(viewModel.isNewPhoneVerify()) {
            if (mobileAuthInfoVO.is_reg == 1) {
                ToastHelper.showSnackBar("手机号已存在绑定", Prompt.ERROR);
                viewModel.showExistBindDialog();
            } else {
                viewModel.nextPage();
                viewModel.fetchVerificationCode();
            }
            return;
        }

        //手机身份信息验证API调用后回调处理：验证手机是否已注册等信息,确定其打开的登录框页面顺序
        if(mobileAuthInfoVO.is_reg == 1) {
            if(mobileAuthInfoVO.user_id == UserEntity.self.getId()) {
                viewModel.nextPage();
            } else {
                ToastHelper.showSnackBar("手机号不匹配", Prompt.ERROR);
            }
        } else {
            ToastHelper.showSnackBar("手机号未注册", Prompt.ERROR);
        }
    }

    /**
     * 获取短信验证码并发送到指定手机号码
     * @param phoneNumber
     */
    public void fetchVerificationCode(String phoneNumber) {
        userDataDAL.fetchVerificationCode(phoneNumber);
    }
    @StrategyAnnotation(event = Url.FetchVerificationCode)
    public void fetchVerificationCodeCallback(OkResponseVO dataVO, boolean isCache) {
        ToastHelper.showSnackBar("验证码已发送请注意查收！", Prompt.SUCCESS);
    }

    /**
     * 校验手机短信验证码
     * @param phoneNumber 接受验证码手机号
     * @param verifyCode 短信验证码
     */
    public void checkVerificationCode(String phoneNumber, String verifyCode) {
        userDataDAL.checkVerificationCode(phoneNumber, verifyCode);
    }
    @StrategyAnnotation(event = Url.CheckVerificationCode)
    public void checkVerificationCodeCallback(OkResponseVO dataVO, boolean isCache) {
        ToastHelper.showSnackBar("验证成功！", Prompt.SUCCESS);
        viewModel.nextPage();
    }

    /**
     * 设置更新绑定手机号码
     * @param phoneNumber 新手机号码
     * @param verifyCode 短信验证码
     */
    public void replaceBindMobile(String phoneNumber,String verifyCode) {
        userDataDAL.replaceBindMobile(phoneNumber, verifyCode);
    }
    @StrategyAnnotation(event = Url.ReplaceMobile)
    public void replaceMobileApiCallbackRefresh(OkResponseVO dataVO, boolean isCache) {
        ToastHelper.showSnackBar("更换成功", Prompt.SUCCESS);
        viewModel.nextPage();
        userDataDAL.refreshUserDetailData();
    }
    @StrategyAnnotation(event = Url.myIndex)
    public void requestMyIndexDataCallback(UserDetailVO dataVO, boolean isCache) {
        if(isCache || dataVO == null) return;
        dataVO.updateUserEntity();
    }

    /**
     * 人工更换绑定电话
     * @param phoneNumber 替换号码
     * @param contactInfo 联系方式 /手机/电话/QQ
     * @param appointmentTime 预约时间段
     * @param reason 替换原因
     */
    public void artificialReplaceMobile(String phoneNumber, String contactInfo, String appointmentTime, String reason) {
        userDataDAL.artificialReplaceMobile(phoneNumber, contactInfo, appointmentTime, reason);
    }
    @StrategyAnnotation(event = Url.ArtificialReplaceMobile)
    public void artificialReplaceMobileCallback(OkResponseVO dataVO, boolean isCache) {
        ToastHelper.showSnackBar("提交成功", Prompt.SUCCESS);
        viewModel.nextPage();
    }
}
